package com.doit.day03

object _07_排序算子 {
  def main(args: Array[String]): Unit = {
    val orders: Array[ScalaOrder] = Array(
      ScalaOrder("001", "001", "001", "004", 25, 36),
      ScalaOrder("001", "002", "002", "007", 324, 44),
      ScalaOrder("002", "003", "003", "005", 84, 3),
      ScalaOrder("002", "004", "004", "004", 345, 53),
      ScalaOrder("002", "005", "005", "005", 869, 547),
      ScalaOrder("006", "006", "006", "005", 234, 43),
      ScalaOrder("007", "007", "007", "007", 4441, 168),
      ScalaOrder("008", "008", "008", "006", 656, 43),
      ScalaOrder("008", "009", "009", "006", 112, 75),
      ScalaOrder("010", "010", "010", "005", 312, 43)
    )
/*    orders.sorted(new Ordering[ScalaOrder] {
      override def compare(x: ScalaOrder, y: ScalaOrder) = ???
    })*/

    //用的比较多，比较友好
//    orders.sortBy(order=>order.price*order.goods_num)

    //sorted
    //sortBy
    //sortWith
    //按照品类进行升序排序，如果品类相同，就按照价格的降序排列
    val flag = true
    val orders1: Array[ScalaOrder] = orders.sortWith((o1, o2) => {
      o1.category_id.compare(o2.category_id) < 0 ||
      o1.category_id.compare(o2.category_id) == 0 && (o1.price*o1.goods_num).compare(o2.price*o2.goods_num)>0
      /*if(o1.category_id.compare(o2.category_id) < 0){
        true
      }else if(o1.category_id.compare(o2.category_id) == 0){
        if((o1.price*o1.goods_num).compare(o2.price*o2.goods_num)>0){
          true
        }else{
          false
        }
      }else{
        false
      }*/
    })

    orders1.foreach(println)

/*    val orders1: Array[ScalaOrder] = orders.sortBy(order => (order.category_id, -order.price * order.goods_num))

    orders1.foreach(println)*/






  }

}
